﻿using HPSocketCS;
using McsUtility;
using McsUtility.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NodeServer.Common
{
    public partial class TcpForwardServer
    {
        /// <summary>
        /// 得到可连接NodeServer
        /// </summary>
        public HandleResult CommServerGetNodeServer(IntPtr connId, byte[] body)
        {
            try
            {
                var m = ProtocolHelper.ReadPackageBody<GetNodeServerResponseModel>(body);
                var response = ProtocolHelper.WritePackage<GetNodeServerResponseModel>(ProtoType.MinerReAccept, m);
                bool a=server.Send((IntPtr)m.ConnID, response, response.Length);
                logger.Debug(m.ConnID+">"+m.IP+":"+m.Port);
                //AddMsg(" > [得到可连接NodeServer]"); 
            }
            catch (Exception ex)
            {

                logger.Error("CommServerGetNodeServer>" + ex.Message);
            }
            return HandleResult.Ok;
        }
        /// <summary>
        /// 修改挖矿信息
        /// </summary>
        public HandleResult CommServerMinerManagerModifyOrePoolUrl(IntPtr connId, byte[] body)
        {
            try
            {
                var m = ProtocolHelper.ReadPackageBody<MinersMiningRequestModel>(body);
                foreach (int item in m.MinersIDs)
                {
                    int ip;
                    if (minersConnID.TryGetValue(item, out ip))
                    {
                        var response = ProtocolHelper.WritePackage(ProtoType.NodeServerMinerManagerModifyOrePoolUrl, body);
                        server.Send((IntPtr)ip, response, response.Length);
                        MCSSQL.DBUtility.DAL.AddMinerLog(item, "修改配置：" +
                            "是否自动矿工名：" + (m.AutoName ? "是" : "不是") + "," +
                            "是否自动启动：" + (m.AutoRun ? "是" : "不是") + "," +
                            "是否自动更新：" + (m.AutoUpdate ? "是" : "不是") + "," +
                            "高级参数：" + m.AdvParams + "," +
                            "备用矿池：" + m.BackupPool + "," +
                            "高级参数：" + m.Dcri + "," +
                            "高级参数：" + m.Eres + "," +
                            "主挖币种：" + m.FirstCoin + "," +
                            "主挖矿池：" + m.FirstPool + "," +
                            "主挖钱包：" + m.FirstWallet + "," +
                            //"最大风扇转数：" + m.MaxFan + "," +
                            "最高温度：" + m.MaxTemp + "," +
                            "矿工名称：" + m.MinerName + "," +
                            "最小风扇转数：" + m.MinFan + "," +
                            "最低算力预警：" + m.MinSpeed + "," +
                            //"最低温度：" + m.MinTemp + "," +
                            "是否双挖：" + (m.Second ? "是" : "不是") + "," +
                            "副挖币种：" + m.SecondCoin + "," +
                            "副挖矿池：" + m.SecondPool + "," +
                            "副挖钱包：" + m.SecondWallet + "," +
                            "高温策略：" + (m.HighTempStrategy == HighTempStrategy.NoOperation ? "无操作" : "降低强度") + "," +
                            "高温报警：" + (m.HighTempWarn ? "是" : "不是") + "," +
                            "低算力报警：" + (m.LowStrengthWarn ? "是" : "不是") + "," +
                            "内核版本：" + (m.CoreType == CoreType.Core98 ? "9.8内核" : (m.CoreType == CoreType.Core114 ? "11.4内核" : (m.CoreType == CoreType.Core115 ? "11.5内核" : "11.6内核"))) + "," +
                            "");
                    }
                }
                //AddMsg(" > [修改挖矿信息]");
            }
            catch (Exception ex)
            {

                logger.Error("CommServerMinerManagerModifyOrePoolUrl>" + ex.Message);
            }
            return HandleResult.Ok;
        }
        /// <summary>
        /// 批量修改挖矿信息
        /// </summary>
        public HandleResult CommServerMinerManagerModifyBatch(IntPtr connId, byte[] body)
        {
            try
            {
                var m = ProtocolHelper.ReadPackageBody<MinersMiningRequestModel>(body);
                foreach (int item in m.MinersIDs)
                {
                    int ip;
                    if (minersConnID.TryGetValue(item, out ip))
                    {
                        var response = ProtocolHelper.WritePackage(ProtoType.NodeServerMinerManagerModifyBatch, body);
                        server.Send((IntPtr)ip, response, response.Length);
                    }
                }
                //AddMsg(" > [批量修改挖矿信息]");
            }
            catch (Exception ex)
            {

                logger.Error("CommServerMinerManagerModifyBatch>" + ex.Message);
            }
            return HandleResult.Ok;
        }
        /// <summary>
        /// 开始挖矿
        /// </summary>
        public HandleResult CommServerMinerManagerStartMining(IntPtr connId, byte[] body)
        {
            try
            {
                var m = ProtocolHelper.ReadPackageBody<int[]>(body);
                foreach (int item in m)
                {
                    int ip;
                    if (minersConnID.TryGetValue(item, out ip))
                    {
                        var response = ProtocolHelper.WritePackage<int>(ProtoType.NodeServerMinerManagerStartMining, item);
                        server.Send((IntPtr)ip, response, response.Length);
                    }
                }
                //AddMsg(" > [开始挖矿]");
            }
            catch (Exception ex)
            {

                logger.Error("CommServerMinerManagerStartMining>" + ex.Message);
            }
            return HandleResult.Ok;
        }
        /// <summary>
        /// 停止挖矿
        /// </summary>
        public HandleResult CommServerMinerManagerEndMining(IntPtr connId, byte[] body)
        {
            try
            {
                var m = ProtocolHelper.ReadPackageBody<int[]>(body);
                foreach (int item in m)
                {
                    int ip;
                    if (minersConnID.TryGetValue(item, out ip))
                    {
                        var response = ProtocolHelper.WritePackage<int>(ProtoType.NodeServerMinerManagerEndMining, item);
                        server.Send((IntPtr)ip, response, response.Length);
                    }
                }
                //AddMsg(" > [停止挖矿]");
            }
            catch (Exception ex)
            {

                logger.Error("CommServerMinerManagerEndMining>" + ex.Message);
            }
            return HandleResult.Ok;
        }
        /// <summary>
        /// 重启
        /// </summary>
        public HandleResult CommServerMinerManagerReset(IntPtr connId, byte[] body)
        {
            try
            {
                var m = ProtocolHelper.ReadPackageBody<int[]>(body);
                foreach (int item in m)
                {
                    int ip;
                    if (minersConnID.TryGetValue(item, out ip))
                    {
                        var response = ProtocolHelper.WritePackage<int>(ProtoType.NodeServerMinerManagerReset, item);
                        server.Send((IntPtr)ip, response, response.Length);
                        MCSSQL.DBUtility.DAL.AddMinerLog(item, "重启机器！");
                    }
                }
                //AddMsg(" > [重启]");
            }
            catch (Exception ex)
            {

                logger.Error("CommServerMinerManagerReset>" + ex.Message);
            }
            return HandleResult.Ok;
        }
        /// <summary>
        /// CommServer下发矿机离线信息给NodeServer
        /// </summary>
        public HandleResult CommServerMinerLogoutToNodeServer(IntPtr connId, byte[] body)
        {
            try
            {
                var m = ProtocolHelper.ReadPackageBody<MinerCloseModel>(body);

                var response = ProtocolHelper.WritePackage(ProtoType.NodeServerMinerLogoutToManager, body);
                server.Send((IntPtr)m.connID, response, response.Length);
                //AddMsg(" > [重启]");
            }
            catch (Exception ex)
            {

                logger.Error("CommServerMinerLogoutToNodeServer>" + ex.Message);
            }
            return HandleResult.Ok;
        }
        /// <summary>
        /// CommonServer下发矿机登录信息给NodeServer
        /// </summary>
        public HandleResult CommServerMinerLoginToNodeServer(IntPtr connId, byte[] body)
        {
            try
            {
                var m = ProtocolHelper.ReadPackageBody<LoginResultForCommServerModel>(body);

                var response = ProtocolHelper.WritePackage(ProtoType.NodeServerMinerLoginToManager, body);
                server.Send((IntPtr)m.connID, response, response.Length);
                //AddMsg(" > [重启]");
            }
            catch (Exception ex)
            {

                logger.Error("CommServerMinerLoginToNodeServer>" + ex.Message);
            }
            return HandleResult.Ok;
        }
        /// <summary>
        /// 矿机挖矿状态下发给NodeServer
        /// </summary>
        public HandleResult MinerClientStartMiningToNodeServer(IntPtr connId, byte[] body)
        {
            try
            {
                var m = ProtocolHelper.ReadPackageBody<MinerClientStatusModel>(body);

                var response = ProtocolHelper.WritePackage(ProtoType.MinerClientStartMiningToManager, body);
                server.Send((IntPtr)m.ConnId, response, response.Length);
                logger.Info("MinerClientStartMiningToNodeServer>" + m.ConnId);
                //AddMsg(" > [重启]");
            }
            catch (Exception ex)
            {

                logger.Error("MinerClientStartMiningToNodeServer>" + ex.Message);
            }
            return HandleResult.Ok;
        }
        /// <summary>
        /// 矿机挖矿状态下发给NodeServer
        /// </summary>
        public HandleResult MinerClientEndMiningToNodeServer(IntPtr connId, byte[] body)
        {
            try
            {
                var m = ProtocolHelper.ReadPackageBody<MinerCloseModel>(body);

                var response = ProtocolHelper.WritePackage(ProtoType.MinerClientEndMiningToManager, body);
                server.Send((IntPtr)m.connID, response, response.Length);
                //logger.Debug("MinerClientEndMiningToNodeServer>" + m.connID + ">" + m.MinerID + ">" + m.MinerName + ">" + m.UserID);

                //AddMsg(" > [重启]");
            }
            catch (Exception ex)
            {

                logger.Error("MinerClientEndMiningToNodeServer>" + ex.Message);
            }
            return HandleResult.Ok;
        }
        /// <summary>
        /// 新管理端登录踢掉老管理端
        /// </summary>
        public HandleResult MinerManagerDownToNodeServer(IntPtr connId, byte[] body)
        {
            try
            {
                var m = ProtocolHelper.ReadPackageBody<int>(body);

                var response = ProtocolHelper.WritePackage(ProtoType.MinerManagerDownToManager, body);
                server.Send((IntPtr)m, response, response.Length);
                //logger.Debug("MinerClientEndMiningToNodeServer>" + m.connID + ">" + m.MinerID + ">" + m.MinerName + ">" + m.UserID);

                //AddMsg(" > [重启]");
            }
            catch (Exception ex)
            {

                logger.Error("MinerManagerDownToNodeServer>" + ex.Message);
            }
            return HandleResult.Ok;
        }
        /// <summary>
        /// 掉线通知NodeServer
        /// </summary>
        public HandleResult MinerCloseToNodeServerr(IntPtr connId, byte[] body)
        {
            try
            {
                var m = ProtocolHelper.ReadPackageBody<LoginResultForCommServerModel>(body);
                MCSSQL.DBUtility.DAL.MinersRequest(3, m.MinerID, m.UserID);
                MCSSQL.DBUtility.DAL.AddMinerLog(m.MinerID, "掉线！");

            }
            catch (Exception ex)
            {

                logger.Error("MinerCloseToNodeServerr>" + ex.Message);
            }
            return HandleResult.Ok;
        }
        public HandleResult CommServerWarnToNodeServer(IntPtr connId, byte[] body)
        {
            try
            {
                var m = ProtocolHelper.ReadPackageBody<WarnModel>(body);
                var response = ProtocolHelper.WritePackage(ProtoType.NodeServerWarnToMinerManager, body);
                server.Send((IntPtr)m.ConnID, response, response.Length);
            }
            catch (Exception ex)
            {

                logger.Error("CommServerWarnToNodeServer>" + ex.Message);
            }
            return HandleResult.Ok;
        }

    }
}
